package com.zfei.gfb.util.gson;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;

import java.util.Arrays;
import java.util.Collection;

/**
 * @author fgh
 */
public class GsonIgnoreStrategy implements ExclusionStrategy {

    private Collection<String> views;

    public GsonIgnoreStrategy(String... views) {
        this(Arrays.asList(views));
    }

    public GsonIgnoreStrategy(Collection<String> views) {
        this.views = views;
    }

    @Override
    public boolean shouldSkipField(FieldAttributes f) {
        GsonIgnore annotation = f.getAnnotation(GsonIgnore.class);
        if (annotation != null) {
            if (views == null || views.isEmpty()) {
                return true;
            }

            String[] value = annotation.value();
            for (String v : value) {
                if (views.contains(v)) {
                    return true;
                }
            }
        }

        return false;
    }

    @Override
    public boolean shouldSkipClass(Class<?> clazz) {
        return false;
    }
}
